﻿--ToT
local ToTFrame = CreateFrame("Button", "ToTFrame", TargetFrame, "SecureUnitButtonTemplate, SecureHandlerAttributeTemplate");
ToTFrame:SetFrameLevel(8);
ToTFrame:SetWidth(96);
ToTFrame:SetHeight(48);
ToTFrame:ClearAllPoints();
ToTFrame:SetPoint("LEFT", TargetFrame, "RIGHT", -20, 0);
ToTFrame:SetMovable(1);

ToTFrame:SetAttribute("unit", "targettarget");
RegisterUnitWatch(ToTFrame);
ToTFrame:SetAttribute("*type1", "target");
ToTFrame:RegisterForClicks("AnyUp");
ToTFrame:RegisterForDrag("LeftButton");

ToTFrame.Portrait = ToTFrame:CreateTexture("ToTPortrait", "BORDER");
ToTFrame.Portrait:SetWidth(27);
ToTFrame.Portrait:SetHeight(27);
ToTFrame.Portrait:ClearAllPoints();
ToTFrame.Portrait:SetPoint("TOPLEFT", ToTFrame, "TOPLEFT", 6, -5);

ToTFrame.Texture = ToTFrame:CreateTexture("ToTTexture", "ARTWORK");
ToTFrame.Texture:SetTexture("Interface\\TargetingFrame\\UI-PartyFrame");
ToTFrame.Texture:SetWidth(96);
ToTFrame.Texture:SetHeight(48);
ToTFrame.Texture:ClearAllPoints();
ToTFrame.Texture:SetPoint("TOPLEFT", ToTFrame, "TOPLEFT", 0, -2);

ToTFrame.Name = ToTFrame:CreateFontString("ToTName", "ARTWORK", "GameFontNormalSmall");
ToTFrame.Name:ClearAllPoints();
ToTFrame.Name:SetPoint("BOTTOMLEFT", ToTFrame, "BOTTOMLEFT", 33, 39);

ToTFrame.HealthBar = CreateFrame("StatusBar", "ToTHealthBar", ToTFrame);
ToTFrame.HealthBar:SetStatusBarTexture("Interface\\TargetingFrame\\UI-StatusBar");
ToTFrame.HealthBar:SetFrameLevel(2);
ToTFrame.HealthBar:SetMinMaxValues(0, 100);
ToTFrame.HealthBar:SetValue(0);
ToTFrame.HealthBar:SetWidth(53);
ToTFrame.HealthBar:SetHeight(6);
ToTFrame.HealthBar:ClearAllPoints();
ToTFrame.HealthBar:SetPoint("TOPLEFT", ToTFrame, "TOPLEFT", 35, -9);
ToTFrame.HealthBar:SetStatusBarColor(0, 1, 0);

ToTFrame.HPPct = ToTFrame:CreateFontString("ToTHPPct", "ARTWORK", "TextStatusBarText");
ToTFrame.HPPct:SetFont(GameFontNormal:GetFont(), 10, "OUTLINE");
ToTFrame.HPPct:SetTextColor(1, 0.75, 0);
ToTFrame.HPPct:SetJustifyH("LEFT");
ToTFrame.HPPct:ClearAllPoints();
ToTFrame.HPPct:SetPoint("LEFT", ToTFrame.HealthBar, "RIGHT", 2, -4);

ToTFrame.PowerBar = CreateFrame("StatusBar", "ToTPowerBar", ToTFrame);
ToTFrame.PowerBar:SetStatusBarTexture("Interface\\TargetingFrame\\UI-StatusBar");
ToTFrame.PowerBar:SetFrameLevel(2);
ToTFrame.PowerBar:SetMinMaxValues(0, 100);
ToTFrame.PowerBar:SetValue(0);
ToTFrame.PowerBar:SetWidth(53);
ToTFrame.PowerBar:SetHeight(6);
ToTFrame.PowerBar:ClearAllPoints();
ToTFrame.PowerBar:SetPoint("TOPLEFT", ToTFrame, "TOPLEFT", 35, -16);
ToTFrame.PowerBar:SetStatusBarColor(0, 0, 1);

local tot = CreateFrame("Frame");
local ToTNameColor = PowerBarColor["MANA"];
local ToTPctText = "";
function UnitFramesPlus_TargetTarget()
    UnitFramesPlus_ToTAttribute();
    if UnitFramesPlusDB["target"]["tot"] == 1 then
        tot:SetScript("OnUpdate", function(self, elapsed)
            self.timer = (self.timer or 0) + elapsed;
            if self.timer >= 0.1 then
                if UnitExists("targettarget") then
                    ToTFrame.Name:SetText(UnitName("targettarget"));

                    ToTNameColor = PowerBarColor[UnitPowerType("targettarget")] or PowerBarColor["MANA"];
                    ToTFrame.PowerBar:SetStatusBarColor(ToTNameColor.r, ToTNameColor.g, ToTNameColor.b);

                    SetPortraitTexture(ToTFrame.Portrait, "targettarget");

                    if UnitHealthMax("targettarget") > 0 then
                        ToTFrame.HealthBar:SetValue(UnitHealth("targettarget") / UnitHealthMax("targettarget") * 100);
                        ToTPctText = "";
                        if UnitFramesPlusDB["target"]["tothppct"] == 1 then
                            ToTPctText = math.floor(UnitHealth("targettarget") / UnitHealthMax("targettarget") * 100).."%";
                        end
                        ToTFrame.HPPct:SetText(ToTPctText);
                    else
                        ToTFrame.HealthBar:SetValue(0);
                        ToTFrame.HPPct:SetText("");
                    end

                    if UnitPowerMax("targettarget") > 0 then
                        ToTFrame.PowerBar:SetValue(UnitPower("targettarget") / UnitPowerMax("targettarget") * 100);
                    else
                        ToTFrame.PowerBar:SetValue(0);
                    end
                else
                    ToTFrame.HealthBar:SetValue(0);
                    ToTFrame.PowerBar:SetValue(0);
                    ToTFrame.HPPct:SetText("");
                end
                self.timer = 0;
            end
        end);
    else
        ToTFrame.HealthBar:SetValue(0);
        ToTFrame.PowerBar:SetValue(0);
        ToTFrame.HPPct:SetText("");
        tot:SetScript("OnUpdate", nil);
    end
end

local function UnitFramesPlus_ToTAttributeSet()
    if UnitFramesPlusDB["target"]["tot"] == 1 then
        ToTFrame:SetAttribute("unit", "targettarget");
    else
        ToTFrame:SetAttribute("unit", nil);
    end
end

function UnitFramesPlus_ToTAttribute()
    if not InCombatLockdown() then
        UnitFramesPlus_ToTAttributeSet();
    else
        local func = {};
        func.name = "UnitFramesPlus_ToTAttributeSet";
        func.callback = function()
            UnitFramesPlus_ToTAttributeSet();            
        end;
        UnitFramesPlus_WaitforCall(func);
    end
end

--非战斗状态中允许shift+左键拖动ToT头像
local function UnitFramesPlus_TargetTargetPositionSet()
    if UnitFramesPlusVar["target"]["tmoved"] == 1 then
        ToTFrame:ClearAllPoints();
        ToTFrame:SetPoint("BOTTOMLEFT", TargetFrame, "BOTTOMLEFT", UnitFramesPlusVar["target"]["totx"], UnitFramesPlusVar["target"]["toty"]);
    else
        ToTFrame:ClearAllPoints();
        ToTFrame:SetPoint("LEFT", TargetFrame, "RIGHT", -20, 0);
    end
end

local function UnitFramesPlus_TargetTargetPosition()
    if not InCombatLockdown() then
        UnitFramesPlus_TargetTargetPositionSet();
    else
        local func = {};
        func.name = "UnitFramesPlus_TargetTargetPositionSet";
        func.callback = function()
            UnitFramesPlus_TargetTargetPositionSet();        
        end;
        UnitFramesPlus_WaitforCall(func);
    end
end

local function UnitFramesPlus_TargetTargetShiftDrag()
    ToTFrame:SetScript("OnMouseDown", function(self, elapsed)
        if UnitFramesPlusDB["target"]["movable"] == 1 then
            if IsShiftKeyDown() and (not InCombatLockdown()) then
                ToTFrame:StartMoving();
                UnitFramesPlusVar["target"]["tmoving"] = 1;
            end
        end
    end)

    ToTFrame:SetScript("OnMouseUp", function(self, elapsed)
        if UnitFramesPlusVar["target"]["tmoving"] == 1 then
            ToTFrame:StopMovingOrSizing();
            UnitFramesPlusVar["target"]["tmoving"] = 0;
            UnitFramesPlusVar["target"]["tmoved"] = 1;
            local bottom = ToTFrame:GetBottom();
            local left = ToTFrame:GetLeft();
            local scale = ToTFrame:GetScale()*TargetFrame:GetScale();
            local bottomX = TargetFrame:GetBottom();
            local leftX = TargetFrame:GetLeft();
            local scaleX = TargetFrame:GetScale();
            UnitFramesPlusVar["target"]["totx"] = (left*scale-leftX*scaleX)/scale;
            UnitFramesPlusVar["target"]["toty"] = (bottom*scale-bottomX*scaleX)/scale;
            ToTFrame:ClearAllPoints();
            ToTFrame:SetPoint("BOTTOMLEFT", TargetFrame, "BOTTOMLEFT", UnitFramesPlusVar["target"]["totx"], UnitFramesPlusVar["target"]["toty"]);
        end
    end)

    ToTFrame:SetClampedToScreen(1);

    --重置目标位置时同时重置ToT位置
    hooksecurefunc("TargetFrame_ResetUserPlacedPosition", function()
        UnitFramesPlusVar["target"]["tmoved"] = 0;
        UnitFramesPlus_TargetTargetPosition();
    end)
end

--ToT头像缩放
local function UnitFramesPlus_TargetTargetScaleSet(oldscale, newscale)
    local oldscale = oldscale or UnitFramesPlusDB["target"]["tscale"];
    local newscale = newscale or UnitFramesPlusDB["target"]["tscale"];
    if UnitFramesPlusDB["target"]["tot"] == 1 then
        local point, relativeTo, relativePoint, offsetX, offsetY = ToTFrame:GetPoint();
        ToTFrame:SetScale(newscale);
        ToTFrame:ClearAllPoints();
        ToTFrame:SetPoint(point, relativeTo, relativePoint, offsetX*oldscale/newscale, offsetY*oldscale/newscale);
        if UnitFramesPlusDB["target"]["totot"] == 1 then
            ToToTFrame:SetScale(newscale);
        end
    end
end

function UnitFramesPlus_TargetTargetScale(oldscale, newscale)
    if not InCombatLockdown() then
        UnitFramesPlus_TargetTargetScaleSet(oldscale, newscale);
    else
        local func = {};
        func.name = "UnitFramesPlus_TargetTargetScaleSet";
        func.callback = function()
            UnitFramesPlus_TargetTargetScaleSet(oldscale, newscale);            
        end;
        UnitFramesPlus_WaitforCall(func);
    end
end

--ToT debuff
local id = 1;
local UFP_MAX_TOT_DEBUFFS = 4;
for id = 1, UFP_MAX_TOT_DEBUFFS, 1 do
    local debuff;
    debuff = CreateFrame("Button", "ToTFrameDebuff"..id, ToTFrame);
    debuff:SetWidth(15);
    debuff:SetHeight(15);
    debuff:SetID(id);
    debuff:ClearAllPoints();
    if id == 1 then
        debuff:SetPoint("BOTTOMLEFT", ToTFrame.Name, "TOPLEFT", -1, 5);
    else
        debuff:SetPoint("LEFT", "ToTFrameDebuff"..id-1, "RIGHT", 2, 0);
    end
    debuff:SetAttribute("unit", "targettarget");
    RegisterUnitWatch(debuff);

    debuff.Icon = debuff:CreateTexture("ToTFrameDebuff"..id.."Icon", "ARTWORK");
    debuff.Icon:SetAllPoints(debuff);

    debuff.Border = debuff:CreateTexture("ToTFrameDebuff"..id.."Border", "OVERLAY");
    debuff.Border:SetTexture("Interface\\Buttons\\UI-Debuff-Overlays");
    debuff.Border:SetWidth(17);
    debuff.Border:SetHeight(17);
    debuff.Border:SetTexCoord(0.296875, 0.5703125, 0, 0.515625);
    debuff.Border:ClearAllPoints();
    debuff.Border:SetPoint("TOPLEFT", debuff, "TOPLEFT", -1, 1);

    debuff:SetScript("OnEnter",function(self)
        GameTooltip:SetOwner(self, "ANCHOR_BOTTOMRIGHT", 20, -20);
        GameTooltip:SetUnitDebuff("targettarget", id);
    end)
    debuff:SetScript("OnLeave",function()
        GameTooltip:Hide();
    end)
end

local totdb = CreateFrame("Frame");
function UnitFramesPlus_TargetTargetDebuff()
    local _G = _G;
    UnitFramesPlus_ToTDebuff();
    if UnitFramesPlusDB["target"]["debuff"] == 1 then
        totdb:SetScript("OnUpdate", function(self, elapsed)
            self.timer = (self.timer or 0) + elapsed;
            if self.timer >= 0.1 then
                for id = 1, UFP_MAX_TOT_DEBUFFS do
                    local _, _, icon = UnitDebuff("targettarget", id);
                    if icon then
                        _G["ToTFrameDebuff"..id].Icon:SetTexture(icon);
                        _G["ToTFrameDebuff"..id]:SetAlpha(1);
                    else
                        _G["ToTFrameDebuff"..id]:SetAlpha(0);
                    end

                end
                self.timer = 0;
            end
        end)
    else
        totdb:SetScript("OnUpdate", nil);
    end
end

local function UnitFramesPlus_ToTDebuffSet()
    if UnitFramesPlusDB["target"]["debuff"] == 1 then
        for id = 1, UFP_MAX_TOT_DEBUFFS, 1 do
            _G["ToTFrameDebuff"..id]:SetAttribute("unit", "targettarget");
        end
    else
        for id = 1, UFP_MAX_TOT_DEBUFFS, 1 do
            _G["ToTFrameDebuff"..id]:SetAttribute("unit", nil);
        end
    end
end

function UnitFramesPlus_ToTDebuff()
    if not InCombatLockdown() then
        UnitFramesPlus_ToTDebuffSet();
    else
        local func = {};
        func.name = "UnitFramesPlus_ToTDebuffSet";
        func.callback = function()
            UnitFramesPlus_ToTDebuffSet();            
        end;
        UnitFramesPlus_WaitforCall(func);
    end
end

--ToToT
local ToToTFrame = CreateFrame("Button", "ToToTFrame", TargetFrame, "SecureUnitButtonTemplate, SecureHandlerAttributeTemplate");
ToToTFrame:SetFrameLevel(8);
ToToTFrame:SetWidth(96);
ToToTFrame:SetHeight(48);
ToToTFrame:ClearAllPoints();
ToToTFrame:SetPoint("TOP", ToTFrame, "BOTTOM", 26, 16);

ToToTFrame:SetAttribute("unit", "targettargettarget");
RegisterUnitWatch(ToToTFrame);
ToToTFrame:SetAttribute("*type1", "targettarget");
ToToTFrame:RegisterForClicks("AnyUp");

ToToTFrame.Portrait = ToToTFrame:CreateTexture("ToTPortrait", "BORDER");
ToToTFrame.Portrait:SetWidth(27);
ToToTFrame.Portrait:SetHeight(27);
ToToTFrame.Portrait:ClearAllPoints();
ToToTFrame.Portrait:SetPoint("TOPLEFT", ToToTFrame, "TOPLEFT", 6, -5);

ToToTFrame.Texture = ToToTFrame:CreateTexture("ToTTexture", "ARTWORK");
ToToTFrame.Texture:SetTexture("Interface\\TargetingFrame\\UI-PartyFrame");
ToToTFrame.Texture:SetWidth(96);
ToToTFrame.Texture:SetHeight(48);
ToToTFrame.Texture:ClearAllPoints();
ToToTFrame.Texture:SetPoint("TOPLEFT", ToToTFrame, "TOPLEFT", 0, -2);

ToToTFrame.Name = ToToTFrame:CreateFontString("ToTName", "ARTWORK", "GameFontNormalSmall");
ToToTFrame.Name:ClearAllPoints();
ToToTFrame.Name:SetPoint("BOTTOMLEFT", ToToTFrame, "BOTTOMLEFT", 33, 39);

ToToTFrame.HealthBar = CreateFrame("StatusBar", "ToTHealthBar", ToToTFrame);
ToToTFrame.HealthBar:SetStatusBarTexture("Interface\\TargetingFrame\\UI-StatusBar");
ToToTFrame.HealthBar:SetFrameLevel(2);
ToToTFrame.HealthBar:SetMinMaxValues(0, 100);
ToToTFrame.HealthBar:SetValue(0);
ToToTFrame.HealthBar:SetWidth(53);
ToToTFrame.HealthBar:SetHeight(6);
ToToTFrame.HealthBar:ClearAllPoints();
ToToTFrame.HealthBar:SetPoint("TOPLEFT", ToToTFrame, "TOPLEFT", 35, -9);
ToToTFrame.HealthBar:SetStatusBarColor(0, 1, 0);

ToToTFrame.PowerBar = CreateFrame("StatusBar", "ToTPowerBar", ToToTFrame);
ToToTFrame.PowerBar:SetStatusBarTexture("Interface\\TargetingFrame\\UI-StatusBar");
ToToTFrame.PowerBar:SetFrameLevel(2);
ToToTFrame.PowerBar:SetMinMaxValues(0, 100);
ToToTFrame.PowerBar:SetValue(0);
ToToTFrame.PowerBar:SetWidth(53);
ToToTFrame.PowerBar:SetHeight(6);
ToToTFrame.PowerBar:ClearAllPoints();
ToToTFrame.PowerBar:SetPoint("TOPLEFT", ToToTFrame, "TOPLEFT", 35, -16);
ToToTFrame.PowerBar:SetStatusBarColor(0, 0, 1);

local totot = CreateFrame("Frame");
local ToToTNameColor = PowerBarColor["MANA"];
function UnitFramesPlus_TargetTargetTarget()
    UnitFramesPlus_ToToTAttribute();
    if UnitFramesPlusDB["target"]["tot"] == 1 and UnitFramesPlusDB["target"]["totot"] == 1 then
        totot:SetScript("OnUpdate", function(self, elapsed)
            self.timer = (self.timer or 0) + elapsed;
            if self.timer >= 0.1 then
                ToToTFrame.HealthBar:SetValue(0);
                ToToTFrame.PowerBar:SetValue(0);
                if UnitExists("targettargettarget") then
                    ToToTFrame.Name:SetText(UnitName("targettargettarget"));

                    ToToTNameColor = PowerBarColor[UnitPowerType("targettargettarget")] or PowerBarColor["MANA"];
                    ToToTFrame.PowerBar:SetStatusBarColor(ToToTNameColor.r, ToToTNameColor.g, ToToTNameColor.b);

                    SetPortraitTexture(ToToTFrame.Portrait, "targettargettarget");

                    if UnitHealthMax("targettargettarget") == 0 then
                        ToToTFrame.HealthBar:SetValue(0);
                    else
                        ToToTFrame.HealthBar:SetValue(UnitHealth("targettargettarget") / UnitHealthMax("targettargettarget") * 100);
                    end

                    if UnitPowerMax("targettargettarget") == 0 then
                        ToToTFrame.PowerBar:SetValue(0);
                    else
                        ToToTFrame.PowerBar:SetValue(UnitPower("targettargettarget") / UnitPowerMax("targettargettarget") * 100);
                    end
                else
                    ToToTFrame.HealthBar:SetValue(0);
                    ToToTFrame.PowerBar:SetValue(0);
                end
                self.timer = 0;
            end
        end);
    else
        ToToTFrame.HealthBar:SetValue(0);
        ToToTFrame.PowerBar:SetValue(0);
        totot:SetScript("OnUpdate", nil);
    end
end

function UnitFramesPlus_ToToTAttribute()
    if not InCombatLockdown() then
        UnitFramesPlus_ToToTAttributeSet();
    else
        local func = {};
        func.name = "UnitFramesPlus_ToToTAttributeSet";
        func.callback = function()
            UnitFramesPlus_ToToTAttributeSet();            
        end;
        UnitFramesPlus_WaitforCall(func);
    end
end

function UnitFramesPlus_ToToTAttributeSet()
    if UnitFramesPlusDB["target"]["tot"] == 1 and UnitFramesPlusDB["target"]["totot"] == 1 then
        ToToTFrame:SetAttribute("unit", "targettargettarget");
    else
        ToToTFrame:SetAttribute("unit", nil);
    end
end

--模块初始化
function UnitFramesPlus_TargetTargetInit()
    UnitFramesPlus_TargetTarget();
    UnitFramesPlus_TargetTargetTarget();
    UnitFramesPlus_TargetTargetShiftDrag();
    UnitFramesPlus_TargetTargetDebuff();
end

function UnitFramesPlus_TargetTargetLayout()
    UnitFramesPlus_TargetTargetPosition();
    UnitFramesPlus_TargetTargetScale();
end
